package com.aktiehuis.bean;

import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;

import com.aktiehuis.main.Aktiehuis;
import com.aktiehuis.main.Klant;
import com.aktiehuis.main.Winkel;

@Stateless(mappedName="Aktiehuis")
@TransactionManagement(TransactionManagementType.BEAN)
public class AktiehuisBean implements Aktiehuis {
	@PersistenceUnit(unitName="connector")
	private EntityManagerFactory emf = null;
	
	public AktiehuisBean() {
		emf = Persistence.createEntityManagerFactory( "connector" );
	}
	
	public Winkel getWinkelById( int winkelId ) {
		Winkel winkel = null;
		try {
			EntityManager em = emf.createEntityManager();
			EntityTransaction tx = em.getTransaction();
			tx.begin();
			
			Query query = em.createQuery( "SELECT w FROM WinkelBean w WHERE w.id = :id" );
			query.setParameter( "id", winkelId );
			
			winkel = (WinkelBean)query.getSingleResult();
			
			tx.commit();
		} catch( Exception e ) {
			e.printStackTrace();
		}
		
		return winkel;
	}
	
	public String test() {
		
		return new String( "test" );
	}
	
	public Klant getKlantById( int klantId ) {
		Klant klant = null;
		
		try {
			EntityManager em = emf.createEntityManager();
			EntityTransaction tx = em.getTransaction();
			tx.begin();
			
			klant = em.find( KlantBean.class, klantId );
			
			tx.commit();
		} catch( Exception e ) {
			e.printStackTrace();
		}
		
		return klant;
	}
}
